
import { _decorator, Component, Node } from 'cc';
import PlayerData from '../../Script/Common/PlayerData';
import yyComponent from '../../Script/Common/yyComponent';
import { EventType } from '../../Script/GameSpecial/GameEventType';
import { GlobalEnum } from '../../Script/GameSpecial/GlobalEnum';
import { SignDataCfg } from './SignDataCfg';
import { SignItem } from './SignItem';
const { ccclass, property } = _decorator;

@ccclass('SignCmp')
export class SignCmp extends yyComponent {
    @property(Node)
    btns: Node = null;

    _signDatId = -1;
    _signRecs: { [id: number]: SignItem } = {};
    _isChecked = false; //当天是否签到

    onLoad() {
        //初始化数据
        for (let i = 0; i < this.node.children.length; i++) {
            const e = this.node.children[i];
            let cmp = e.getComponent(SignItem);
            cmp.initData(i, false, this.onItemClick.bind(this));
            this._signRecs[i] = cmp;
        }

    }

    onEnable() {
        this.checkSign();
    }

    checkSign() {
        let data = PlayerData.getPlayerData();
        this._signDatId = data.signData.count;
        //判断是否已签到
        let d1 = new Date(data.signData.lastTime);
        let d2 = new Date();
        this._isChecked = d1.getFullYear() == d2.getFullYear() && d1.getMonth() == d2.getMonth() && d1.getDate() == d2.getDate();
        // this._isChecked = false;//test

        if (data.signData.count >= 6 && !this._isChecked) {
            //重置
            let data = PlayerData.getPlayerData();
            this._signDatId = -1;
            data.signData.lastTime = Date.now();
            data.signData.count = this._signDatId;

            this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
                attribute: "gameData.signData",
                value: data.signData,
                mode: "=",
                emit: true,
                save: true,
            })
        }
        //刷新item
        for (const key in this._signRecs) {
            let isCheck = this._signRecs[key]._dayId <= data.signData.count;
            this._signRecs[key].setCheckState(isCheck);
        }

        this.btns.active = !this._isChecked;
    }

    //签到
    sign() {
        if (this._isChecked) return;
        this._isChecked = true;

        this._signDatId++;
        if (this._signDatId > 6) {
            this._signDatId = 0;
        }
        //存储
        let data = PlayerData.getPlayerData();

        data.signData.lastTime = Date.now();
        data.signData.count = this._signDatId;

        this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
            attribute: "gameData.signData",
            value: data.signData,
            mode: "=",
            emit: true,
            save: true,
        })

        this.checkSign();

        //关闭UI
        // this.emit(EventType.Common.UIEvent.exit, GlobalEnum.UI.signUI);

        //获取奖励
        let cfg = SignDataCfg[this._signDatId];
        if (!cfg) return;
        if (cfg.awardType == 0) {
            //播放金币动画
            this.emit(EventType.Common.UIEvent.playGoldAnim, {
                gold: cfg.val
            });
        }
    }

    onItemClick(id: number) {
        if (this._isChecked) {
            this.emit(EventType.Common.SDKEvent.showMsg, '今天已经签到了哦!');
            return;
        }
        let nextId = this._signDatId + 1;
        nextId = nextId > 6 ? 0 : nextId;

        if (id != nextId) return;
        this.sign();
    }

    onGetAwardClick() {
        this.sign();
    }

    onVideoGetAward() {
        this.emit(EventType.Common.SDKEvent.showVideo, () => {
            this.sign();
        })
    }
}
